﻿using System;
using System.Collections.Generic;
using System.Linq;
using Risk.Model;
using Risk.Services;

namespace ConsoleApplication
{
    internal class Program
    {
        private static void Main()
        {
            //DatabaseManager.CreatePlayer(DatabaseManager.CreateGame(), "marcel", true);
            //DatabaseManager.PlayerHasMovedTroops(78);
            Console.Read();
        }
        /*
        private static void Main2(string[] args)
        {

            // PLAYER REGISTRATION
            Game testGame = new Game();
            string input;
            do
            {
                Console.WriteLine("Enter your player name, press [return] if all players have been added");
                input = Console.ReadLine();
                if (input == "")
                {
                    if (testGame.Players.Count < 2)
                        Console.WriteLine("Not enough players have entered the game yet.");
                }
                else
                    testGame.Players.Add(new Player(input));
            } while (testGame.Players.Count < 2 || (input != "" && testGame.Players.Count() < 6));

            // give regions randomly to players and initialize their initial troop count
            Console.WriteLine("Dividing regions over players");
            Console.ReadLine();
            testGame.InitializeGame();
            Console.WriteLine(testGame.RegionInformation());

            // make each player choose a valid region to send all their initial troops to
            foreach (Player p in testGame.Players)
            {
                bool sentTroops = false;
                while (!sentTroops)
                {
                    Console.WriteLine("{0}, enter the region you wish to send all your initial troops({1}) to ",
                                      p.Name, p.InitialTroops.TroopSize);
                    input = Console.ReadLine();
                    Region chosenRegion = testGame.GetRegion(input);
                    if (chosenRegion == null)
                    {
                        Console.WriteLine("Given region name doesn't exist");
                    }
                    else if (chosenRegion.PlayerId != p.Id)
                    {
                        Console.WriteLine("You don't own the given region: " + chosenRegion.Id);
                    }
                    else
                    {
                        Console.WriteLine("Sent {0} troops to {1}", p.InitialTroops.TroopSize, chosenRegion.Id);
                        sentTroops = true;
                        p.SendInitialTroopsTo(chosenRegion);
                    }
                }
            }

            //main game loop
            bool gameFinished = false;
            while (!gameFinished)
            {
                foreach (Player currentPlayer in testGame.Players)
                {
                    Console.WriteLine("===================================");
                    Console.WriteLine("{0}s turn has started.", currentPlayer.Name);
                    Console.ReadLine();
                    Console.WriteLine(testGame.RegionInformation());
                    Console.WriteLine("----------------------------------");
                    Console.WriteLine("Reinforcements");
                    currentPlayer.PlayerState = PlayerState.Reinforcing;
                    int reinforcementTroops = testGame.GiveReinforcements(currentPlayer);
                    Console.WriteLine("You have received {0} troops", reinforcementTroops);
                    while (currentPlayer.ReinforcementTroops.TroopSize != 0)
                    {
                        Console.WriteLine("You have {0} reinforcement troops available",
                                          currentPlayer.ReinforcementTroops.TroopSize);
                        Console.WriteLine("Choose which region you want to send reinforcements to.");
                        input = Console.ReadLine();
                        if (testGame.PlayerHasRegion(currentPlayer.Id, input))
                        {
                            String targetRegionId = input;
                            Console.WriteLine("Enter the amount of troops you would like to send to {0}", input);
                            input = Console.ReadLine();
                            int amountOfTroops;
                            while (!int.TryParse(input, out amountOfTroops) || amountOfTroops < 1 ||
                                   amountOfTroops > currentPlayer.ReinforcementTroops.TroopSize)
                            {
                                Console.WriteLine("Invalid number of troops");
                                input = Console.ReadLine();
                            }
                            currentPlayer.SendReinforcementsTo(testGame.GetRegion(targetRegionId), amountOfTroops);
                            Console.WriteLine("Sent {0} reinforcements to {1}", amountOfTroops, targetRegionId);
                        }
                        else
                        {
                            Console.WriteLine("You don't own that region.");
                        }
                    }
                    Console.ReadLine();
                    //Console.WriteLine(testGame.RegionInformation());
                    Console.WriteLine("----------------------------------");
                    Console.WriteLine("Attacking");
                    currentPlayer.PlayerState = PlayerState.Attacking;
                    Console.WriteLine("Do you want to attack anyone? y/n");
                    input = Console.ReadLine();
                    Boolean continueAttack = (input == "y");
                    if (continueAttack)
                    {
                        String originRegionId;
                        do
                        {
                            Console.WriteLine(
                                "Choose the region where you want to start the attack from or press [return] if you don't want to attack");
                            originRegionId = Console.ReadLine();
                        } while (originRegionId == "" || !testGame.PlayerHasRegion(currentPlayer.Id, originRegionId) || testGame.GetRegion(originRegionId).Troops.TroopSize == 1);

                        String defenderRegionId;
                        do
                        {
                            Console.WriteLine("Choose the region you want to attack");
                            defenderRegionId = Console.ReadLine();
                        } while (!testGame.GetRegion(originRegionId).Borders(defenderRegionId) ||
                                    testGame.PlayerHasRegion(currentPlayer.Id, defenderRegionId));
                        Region originRegion = testGame.GetRegion(originRegionId);
                        Region defenderRegion = testGame.GetRegion(defenderRegionId);
                        do
                        {
                            Console.WriteLine("Enter the amount of troops you want to attack with");
                            int attackTroops;
                            while (!int.TryParse(Console.ReadLine(), out attackTroops) || attackTroops < 1 ||
                                    attackTroops > 3 || attackTroops > (originRegion.Troops.TroopSize - 1))
                            {
                                Console.WriteLine("Invalid number of troops");
                            }

                            int [] attackerDice = RandomDiceResult.GenerateSortedArray(attackTroops);
                            int[] defenderDice = RandomDiceResult.GenerateSortedArray(((defenderRegion.Troops.TroopSize >= 2) ? 2 : 1));
                            for (int i = 0; i < attackTroops; i++)
                            {
                                Console.WriteLine("Throwing red dice");
                                Console.WriteLine("    Result: " + attackerDice[i]);
                            }
                            for (int i = 0; i < ((defenderRegion.Troops.TroopSize >= 2) ? 2 : 1); i++)
                            {
                                Console.WriteLine("Throwing blue dice");
                                Console.WriteLine("    Result: " + defenderDice[i]);
                            }
                            if (originRegion.Troops.TroopSize > 1 &&
                                defenderRegion.PlayerId != currentPlayer.Id)
                            {
                                Console.WriteLine("Do you want to attack again? (y/n)");
                                input = Console.ReadLine();
                            }
                            else
                            {
                                input = "n";
                            }
                        } while (input != "n" && originRegion.Troops.TroopSize > 1 &&
                                    defenderRegion.PlayerId != currentPlayer.Id);

                        if (defenderRegion.PlayerId == currentPlayer.Id)
                        {
                            Console.WriteLine(
                                "Enter the amount of troops you'd like to move to the region you have just conquered");
                            int numberOfTroopsToMove;
                            while (!int.TryParse(Console.ReadLine(), out numberOfTroopsToMove) ||
                                    numberOfTroopsToMove > (originRegion.Troops.TroopSize - 1) ||
                                    numberOfTroopsToMove < 1)
                            {
                                Console.WriteLine("Invalid number of troops");
                            }
                            originRegion.Troops.Move(defenderRegion, numberOfTroopsToMove);
                            Console.WriteLine("Moved {0} troops from {1} to {2}", numberOfTroopsToMove, originRegionId, defenderRegionId);
                        }
                        Console.ReadLine();
                        Console.WriteLine(testGame.RegionInformation());
                        Console.WriteLine("----------------------------------");
                        Console.WriteLine("Moving troops");
                        currentPlayer.PlayerState = PlayerState.Waiting;
                        Console.WriteLine("Do you want to move troops? (y/n)");
                        input = Console.ReadLine();
                        if (input != "n")
                        {
                            Console.WriteLine("Enter the origin region");
                            String originMoveRegionId = Console.ReadLine();
                            while (testGame.GetRegion(originMoveRegionId) == null ||
                                   testGame.GetRegion(originMoveRegionId).PlayerId != currentPlayer.Id)
                            {
                                Console.WriteLine("Invalid region id");
                                originMoveRegionId = Console.ReadLine();
                            }
                            Region originMoveRegion = testGame.GetRegion(originRegionId);
                            String targetMoveRegionId = Console.ReadLine();
                            while (testGame.GetRegion(targetMoveRegionId) == null ||
                                   testGame.GetRegion(targetMoveRegionId).PlayerId != currentPlayer.Id)
                            {
                                Console.WriteLine("Invalid region id");
                                targetMoveRegionId = Console.ReadLine();
                            }
                            Region targetMoveRegion = testGame.GetRegion(targetMoveRegionId);
                            int nrOfTroopsToMove;
                            while (!int.TryParse(Console.ReadLine(), out nrOfTroopsToMove) || nrOfTroopsToMove < 1 ||
                                   nrOfTroopsToMove > (originRegion.Troops.TroopSize - 1))
                            {
                                Console.WriteLine("Invalid number");
                            }
                            originMoveRegion.Troops.Move(targetMoveRegion, nrOfTroopsToMove);
                            Console.WriteLine("Moved {0} troops from {1} to {2}", nrOfTroopsToMove, originMoveRegionId, targetMoveRegionId);

                        }
                        currentPlayer.PlayerState = PlayerState.Waiting;

                    }
                }
                gameFinished = testGame.GameOver();
                if (gameFinished)
                {
                    Console.WriteLine("Game over");
                    Console.WriteLine("Player {0} has won the game", testGame.Winner().Name);
                }
                else
                    Console.ReadLine();

            }
        }
    */
    }
}
